package logic

import (
	"context"

	"server_1.0/service/student/internal/svc"
	"server_1.0/service/student/student"
	"server_1.0/util"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryIDLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryIDLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryIDLogic {
	return &QueryIDLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *QueryIDLogic) QueryID(in *student.QueryIDReq) (*student.QReps, error) {
	stu, err := l.svcCtx.StudentModel.FindOne(l.ctx, in.No)
	code := util.Success
	usersHandle := make([]*student.Reps, 0)
	count := 0
	if err != nil {
		code = util.NoFoundError
		usersHandle = append(
			usersHandle,
			&student.Reps{
				Code: int64(code),
				ID:   in.No,
			})
		count += 1
		return &student.QReps{
			Count: int64(count),
			QRs:   usersHandle,
		}, nil
	} else {
		code = util.Success
		usersHandle = append(
			usersHandle,
			&student.Reps{
				Code:     int64(code),
				No:       in.No,
				Name:     stu.Sname,
				Sex:      stu.Ssex,
				Age:      stu.Sage,
				Password: stu.Spwd,
			})
		count += 1
		return &student.QReps{
			Count: int64(count),
			QRs:   usersHandle,
		}, nil
	}
}
